﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using IHISCETeim.Library;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using IHISCETeim.Model;
using System.Configuration;
namespace IHISCETeim.DAL
{
    public class SyncSAPDAL
    {
        /// <summary>
        /// SBO连接字符串
        /// </summary>
        protected static string SBOConnStr
        {
            get
            {
                return "SBOConnectionString";
            }
        }
        private static Dictionary<string, string> _ConnStrInfo;
        /// <summary>
        /// 获取SQL联系字符串中的各个项,
        /// 项的标志全部为小写,并去掉项的空格符
        /// 如Data Source已变成datasource
        /// </summary>
        private static Dictionary<string, string> ConnStrInfo
        {
            get
            {
                ///Database DB = DatabaseFactory.CreateDatabase(SBOConnStr);
                if (_ConnStrInfo == null)
                {
                    string SBOconnstr = ConfigurationManager.ConnectionStrings[SBOConnStr].ConnectionString;
                    string[] connectionstring = SBOconnstr.Split(new char[] { ';' });
                    _ConnStrInfo = new Dictionary<string, string>();
                    foreach (string item in connectionstring)
                    {
                        string[] itemarr = item.Split(new char[] { '=' });
                        _ConnStrInfo.Add(itemarr[0].ToLower().Replace(" ", ""), itemarr[1]);
                    }
                }
                return _ConnStrInfo;
            }
        }

        protected static string DataSource {
            get
            {
                return ConnStrInfo["datasource"];
            }
        }
        protected static string InitialCatalog
        {
            get
            {
                return ConnStrInfo["initialcatalog"];
            }
        }
        protected static string UserID
        {
            get
            {
                return ConnStrInfo["userid"];
            }
        }
        protected static string Password
        {
            get
            {
                return ConnStrInfo["password"];
            }
        }
        #region 同步相关
        /// <summary>
        /// 同步SAP 发货记录
        /// </summary>
        public static bool SyncUClaimDemage()
        {
            Database DB = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = DB.GetStoredProcCommand("SAP_SyncUClaimDemage",DataSource,UserID,Password,InitialCatalog);
            try
            {
                DB.ExecuteNonQuery(dbCommand);
                return true;
            }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// 同步代理商列表
        /// </summary>
        /// <returns></returns>
        public static bool SyncAgentList()
        {
            Database DB = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = DB.GetStoredProcCommand("SAP_SyncAgentList", DataSource, UserID, Password, InitialCatalog);
            try
            {
                DB.ExecuteNonQuery(dbCommand);
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 获取所有代理商的信息
        /// </summary>
        /// <returns></returns>
        public static DataTable GetAgentList()
        {
            Database DB = DatabaseFactory.CreateDatabase();
            string sql = "SELECT * FROM dbo.SAP_AgentList WHERE FromSap=0  ORDER BY CardCode";
            // sql = string.Format(sql, cardcode);
            DbCommand dbcomm = DB.GetSqlStringCommand(sql);
            DataTable dt = DB.ExecuteDataSet(dbcomm).Tables[0];
            return dt;
        }

        #endregion
    }
}
